package com.xjrsoft.module.base.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xjrsoft.module.base.dto.WeChatDepartDto;
import com.xjrsoft.module.base.dto.WeChatUserDto;
import com.xjrsoft.module.oa.entity.XjrOaMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class WeChatUtil {
    @Value("${xjrsoft.global-config.enterpriseWeChat.corpid}")
    public String appKey;

    @Value("${xjrsoft.global-config.enterpriseWeChat.secret}")
    public String appSecret;

    @Value("${xjrsoft.global-config.enterpriseWeChat.secret1}")
    public String appSecret1;

    @Value("${xjrsoft.global-config.enterpriseWeChat.agentid}")
    public Integer agentid;
    

    //获取通讯录或发消息token
    public String getToken(Integer type) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("corpid", appKey);
        if(type==1) {
            paramMap.put("corpsecret", appSecret);
        }else if(type==2){
            paramMap.put("corpsecret", appSecret1);
        }
        String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken", paramMap);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String token = jsonObject.get("access_token").toString();
        return token;
    }

    //根据所有部门信息
    public List<WeChatDepartDto> getDepartmentList(Long dept_id) {
        String token = this.getToken(1);
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", token);
        if (dept_id != null) {
            paramMap.put("id", dept_id);
        }
        String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/department/list", paramMap);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Object department = jsonObject.get("department");
        List<WeChatDepartDto> weChatDepartDtos = JSONObject.parseArray(department.toString(), WeChatDepartDto.class);
        return weChatDepartDtos;
    }

    //获取部门的所有用户信息
    public List<WeChatUserDto> getDepartmentUser(Long dept_id) {
        String token = this.getToken(1);
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("access_token", token);
            paramMap.put("department_id", dept_id);
            paramMap.put("fetch_child", 1);
            String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/user/list", paramMap);
          JSONObject jsonObject = JSONObject.parseObject(result);
            Object department = jsonObject.get("userlist");
        List<WeChatUserDto> weChatUserDtos = JSONObject.parseArray(department.toString(), WeChatUserDto.class);
        return weChatUserDtos;
    }

    //发送消息给企业微信用户
    public Boolean sendMessage(XjrOaMessage xjrOaMessage, String userId) {
        String token = this.getToken(2);
        JSONObject object = new JSONObject();
        object.put("touser", userId);
        object.put("msgtype", "text");
        object.put("agentid", agentid);
        JSONObject text = new JSONObject();
        text.put("content", xjrOaMessage.getContent());
        object.put("text",text);
        String result = HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token, JSONObject.toJSONString(object));
        System.out.printf(result);
        return true;
    }
}
